﻿using EyFramework.Common;
using EyFramework.Library;
using Ncov2019.Core.Service;
using Ncov2019.Entity;
using System;
using EyFramework.Sys.Core;

namespace Ncov2019
{
    public partial class FrmNcovPatEdit : FrmDevEditBase
    {
        private INcovUsersService ncovUsersService = Globals.GetService<INcovUsersService>();
        private NcovUsers _ncovUser;

        public FrmNcovPatEdit()
        {
            InitializeComponent();
        }

        public override void InitForm()
        {
            DataBinder.BindingLookupEditDataSource(lueSex, SysCache.GetCacheSysDict(SysConstants.DICT_SEX), "DICT_LABEL", "DICT_VALUE");
            DataBinder.BindingLookupEditDataSource(lueSflb, SysCache.GetCacheSysDict(SysConstants.DICT_NCOV_PAT_TYPE), "DICT_LABEL", "DICT_VALUE");
            base.InitForm();
        }

        public override void DisplayData()
        {
            //1、检索数据
            if (!string.IsNullOrEmpty(ID))
            {
                _ncovUser = ncovUsersService.SelectByPatseq(long.Parse(ID));
                //如果没有上报记录，设置上报的初始值
                if (_ncovUser != null)
                {
                    #region 设置初始值

                    tePatNm.Text = _ncovUser.PAT_NM;
                    teBirthday.Text = _ncovUser.BIRTHDAY;
                    lueSex.EditValue = _ncovUser.SEX;
                    teIdNo.Text = _ncovUser.ID_NO;
                    teTelphone.Text = _ncovUser.TELPHONE;
                    lueSflb.EditValue = _ncovUser.PAT_TYPE;
                    teCompany.Text = _ncovUser.COMPANY;
                    teDepartment.Text = _ncovUser.DEPARTMENT;
                    teSfbq.Text = _ncovUser.PAT_TAGS;
                    teChtId.Text = _ncovUser.CHT_ID;
                    chkDcFlag.Checked = (_ncovUser.DC_FLAG == "1" ? true : false);

                    #endregion 设置初始值
                }
            }
        }

        public override void ClearScreen()
        {
            this.ID = "";
            tePatNm.Text = "";
            teBirthday.Text = "";
            lueSex.EditValue = "";
            teIdNo.Text = "";
            teTelphone.Text = "";
            lueSflb.EditValue = null;
            teCompany.Text = "";
            teDepartment.Text = "";
            teSfbq.Text = "";
            teChtId.Text = "";
            chkDcFlag.Checked = false;
            base.ClearScreen();
        }

        public override bool CheckInput()
        {
            if (string.IsNullOrEmpty(tePatNm.Text.Trim()))
            {
                EyFramework.Common.Globals.ShowWarning("姓名不能为空！");
                return false;
            }
            if (string.IsNullOrEmpty(lueSex.Text))
            {
                EyFramework.Common.Globals.ShowWarning("性别不能为空！");
                return false;
            }
            if (string.IsNullOrEmpty(teIdNo.Text))
            {
                EyFramework.Common.Globals.ShowWarning("身份证号不能为空！");
                return false;
            }
            if (string.IsNullOrEmpty(lueSflb.Text))
            {
                EyFramework.Common.Globals.ShowWarning("身份类别不能为空！");
                return false;
            }
            return true;
        }

        public override bool SaveUpdated()
        {
            var ncovUser = FillData();
            ncovUser.PAT_SEQ = _ncovUser.PAT_SEQ;
            return ncovUsersService.UpdateNcovUser(ncovUser);
        }

        public override bool SaveAddNew()
        {
            return ncovUsersService.AddNcovUser(FillData());
        }

        private NcovUsers FillData()
        {
            NcovUsers ncovUser = new NcovUsers();
            ncovUser.PAT_NM = tePatNm.Text;
            ncovUser.BIRTHDAY = teBirthday.Text;
            ncovUser.SEX = lueSex.EditValue.ToString();
            ncovUser.ID_NO = teIdNo.Text.Trim();
            ncovUser.TELPHONE = teTelphone.Text.Trim();
            ncovUser.PAT_TYPE = lueSflb.EditValue.ToString();
            ncovUser.COMPANY = teCompany.Text.Trim();
            ncovUser.DEPARTMENT = teDepartment.Text.Trim();
            ncovUser.PAT_TAGS = teSfbq.Text.Trim();
            ncovUser.CHT_ID = teChtId.Text.Trim();
            ncovUser.DC_FLAG = chkDcFlag.Checked ? "1" : "0";
            return ncovUser;
        }

        private void teIdNo_EditValueChanged(object sender, EventArgs e)
        {
            if (teIdNo.Text.Trim().Length == 15 || teIdNo.Text.Trim().Length == 18)
            {
                teBirthday.Text = Globals.GetBirthByIdNo(teIdNo.Text);
                if (Globals.GetSexByIdNo(teIdNo.Text).Equals("男"))
                {
                    lueSex.ItemIndex = 0;
                }
                else if (Globals.GetSexByIdNo(teIdNo.Text).Equals("女"))
                {
                    lueSex.ItemIndex = 1;
                }
                //lueSex.EditValue = Globals.GetSexByIdNo(teIdNo.Text);
            }
        }
    }
}